package cn.baiweigang.uu.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesUtil {
    private static Properties properties;


    public static String getProperty(String key) {
        return getProperty(key, null);
    }

    public static String getProperty(String key, String defaultValue) {
        if (properties == null) init();
        return properties.getProperty(key, defaultValue);
    }

    private static void init() {
        try {
            properties = new Properties();
            InputStream inputStream;
            String configPath = System.getProperty("user.dir") + File.separator + "config" + File.separator + "application.properties";
            File configFile = new File(configPath);
            if (configFile.exists()) {
                inputStream = new FileInputStream(configFile);
            } else {
                inputStream = DButil.class.getClassLoader().getResourceAsStream("application.properties");
            }
            properties.load(inputStream);

        } catch (IOException e) {
            LogUtil.logError("读取配置出错 " + e.getMessage());
        }
    }
}
